// registerUser.js
const app = getApp()
Page({

  data: {
    infoData: {},
    enterprise: { registerName: '企业名称', name: '法人姓名', identification: '法人身份证号', phone: '法人联系电话', sfzh: '企业代码' },
    spouse: { registerName: '申请人姓名', name: '配偶姓名', identification: '配偶身份证号', phone: '配偶联系电话', sfzh: "申请人身份证号" },
    marryStatus: wx.getStorageSync('marryStatus'),
    registerName: wx.getStorageSync('registerName'),
    password: '',
    againPassword: '',
    userType: wx.getStorageSync('userType'),
    identification: wx.getStorageSync('identification'),
    registerPhone: wx.getStorageSync('registerPhone'),
    address: wx.getStorageSync('address'),
    spouseName: wx.getStorageSync('spouseName'),
    spouseIdentification: wx.getStorageSync('spouseIdentification'),
    spousePhone: wx.getStorageSync('spousePhone'),

    qylxr: false,
    marriage: true,
    accept: false,
  },
  //个人/单位
  registerChange: function (e) {
    wx.setStorageSync('userType', e.detail.value)
    if (e.detail.value == "0") {
      this.setData({
        userType: wx.getStorageSync('userType'),
        qylxr: true,
        infoData: this.data.spouse
      })
    }
    if (e.detail.value == '1') {
      this.setData({
        userType: wx.getStorageSync('userType'),
        qylxr: false,
        marriage: true,
        infoData: this.data.enterprise
      })
    }
  },
  //名称
  registerName: function (e) {
    var that = this;
    if (e.detail.value != null && e.detail.valuev != '') {
      wx.setStorageSync('registerName', e.detail.value)
      that.setData({
        registerName: wx.getStorageSync('registerName')
      })
    }
  },
  //身份证/企业号
  identification: function (e) {
    var that = this;
    if (that.data.userType == '0') {
      // 验证中文名称 
      // function isChinaName(name) {
      //   var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
      //   return pattern.test(name);
      // }
      // 验证手机号 
      // function isPhoneNo(phone) {
      //   var pattern = /^1[34578]\d{9}$/;
      //   return pattern.test(phone);
      // }
      // 验证身份证
      if (e.detail.value != null && e.detail.value != '') { 
        var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(d|X|x)$)/;
        var identification = e.detail.value;
        var idIsTrue = pattern.test(identification);
        if (idIsTrue) {
          wx.request({
            url: app.globalData.requestURL +'userInfo/' + identification + '/getUserStatusByCondition',
            method: 'POST',

            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success: function (res) {
              if (res.data.status == 1) {
                that.setData({
                  accept: false
                });
                wx.showToast({
                  title: '该用户已注册',
                  duration: 2500,
                  icon: 'loading',
                  mask: true
                });
                wx.setStorageSync('identification', identification)
                that.setData({
                  identification: wx.getStorageSync('identification')
                })
              } else {
                wx.setStorageSync('identification', identification)
                that.setData({
                  identification: wx.getStorageSync('identification')
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '身份证号错误',
            icon: 'loading',
            duration: 3000,
            mask: true
          });
          wx.setStorageSync('identification', '')
          that.setData({
            accept: false,
            identification: wx.getStorageSync('identification')
          })
        }
      }else{
        wx.showToast({
          title: '身份证号不能为空',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
      }
    } else {
      if (e.detail.value != null && e.detail.value != ''){
        var identification = e.detail.value;
        wx.request({
          url: 'https://www.gcjxglzx.com/userInfo/' + identification + '/getUserStatusByCondition',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.status == 1) {
              wx.showToast({
                title: '该用户已注册',
                duration: 2500,
                icon: 'loading',
                mask: true
              });
              wx.setStorageSync('identification', '');
              that.setData({
                accept: false,
                identification: wx.getStorageSync('identification')
              })
            } else {
              wx.setStorageSync('identification', identification)
              that.setData({
                identification: wx.getStorageSync('identification')
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: '企业代码不能为空',
          duration: 2500,
          icon: 'loading',
          mask: true
        });
      }
    }
  },
  //输入的密码
  getPwd: function (e) {
    this.setData({
      password: e.detail.value
    });
  },
  //俩次输入的密码的对比 
  provingPwd: function (e) {
    var password = this.data.password;
    var provingPwd = e.detail.value;
    if (password != provingPwd || password == '') {
      var that = this;
      wx.showModal({
        title: '密码设置错误!',
        content: '密码不能空或俩次输入的密码不一致',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            that.setData({
              accept: false,
              password: '',
              againPassword: ''
            });
          }
        }
      })
    }
  },
  registerPhone: function (e) {
    var that = this;
    // 验证手机号
    var pattern = /^1[34578]\d{9}$/;
    var registerPhone = e.detail.value;
    if (pattern.test(registerPhone)) {
      wx.setStorageSync('registerPhone', registerPhone)
      that.setData({
        registerPhone: wx.getStorageSync('registerPhone')
      })
    } else {
      wx.showToast({
        title: '手机号码有误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      wx.setStorageSync('registerPhone', '')
      that.setData({
        accept: false,
        registerPhone: wx.getStorageSync('registerPhone')
      })
    }
  },
  maritalStatus: function (e) {
    wx.setStorageSync('marryStatus', e.detail.value); //婚姻状况
    this.setData({
      marryStatus: wx.getStorageSync('marryStatus')
    })
    if (e.detail.value == '0') {
      this.setData({
        marriage: true,
      })
    }
    if (e.detail.value == '1') {
      this.setData({
        marriage: false,
      })
    }
    if (e.detail.value == '2') {
      this.setData({
        marriage: false
      })
    }
    if (e.detail.value == '3') {
      this.setData({
        marriage: false
      })
    }
  },
  accept: function (e) {
    if (e.detail.value == '0') {
      this.setData({
        accept: true
      })
    } else {
      this.setData({
        accept: false
      })
    }
  },
  //地址
  address:function(e){
    var that = this;
    if (e.detail.value != null && e.detail.valuev != ''){
      wx.setStorageSync('address', e.detail.value)
      that.setData({
        address: wx.getStorageSync('address')
      })
    }
  },
  //配偶姓名
  spouseName:function(e){
    var that = this;
    if (e.detail.value != null && e.detail.valuev != '') {
      wx.setStorageSync('spouseName', e.detail.value)
      that.setData({
        spouseName : wx.getStorageSync('spouseName')
      })
    }
  },
  //配偶身份证号
  spouseIdentification: function (e) {
    var that = this;
    if (e.detail.value != null && e.detail.valuev != '') {
      wx.setStorageSync('spouseIdentification', e.detail.value)
      that.setData({
        spouseIdentification: wx.getStorageSync('spouseIdentification')
      })
    }
  },
  spousePhone:function(e){
    var that = this;
    if (e.detail.value != null && e.detail.valuev != '') {
      wx.setStorageSync('spousePhone', e.detail.value)
      that.setData({
        spousePhone: wx.getStorageSync('spousePhone')
      })
    }
  },
  //提交
  submit: function (e) {
    var that = this;
    var state = true;
    var marriage = that.data.marriage;
    var userType = e.detail.value.userType
    if (userType == "" || userType == null) {
      state = false;
    }
    var registerName = that.data.registerName
    if (registerName == "" || registerName == null) {
      state = false;
      wx.showToast({
        title: '名称不能为空',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }
    var spouseName = that.data.spouseName
    if (userType == '0' && marriage && (spouseName == '' || spouseName == null)){
      state = false;
      that.setData({
        accept: false
      })
      wx.showToast({
        title: '配偶姓名错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    } else if (userType == '1' &&(spouseName == '' || spouseName == null)){
      state = false;
      that.setData({
        accept: false
      })
      wx.showToast({
        title: '法人姓名错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var spouseIdentification = that.data.spouseIdentification
    if (userType == '0' && marriage && (spouseIdentification == '' || spouseIdentification == null)) {
      state = false;
      that.setData({
        accept: false
      })
      wx.showToast({
        title: '配偶身份证错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    } else if (userType == '1' && (spouseIdentification == '' || spouseIdentification == null)) {
      state = false;
      wx.showToast({
        title: '法人身份证错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }
    var spousePhone = that.data.spousePhone
    if (userType == '0' && marriage && (spousePhone == '' || spousePhone == null)) {
      state = false;
      wx.showToast({
        title: '配偶手机错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    } else if (userType == '1' && (spousePhone == '' || spousePhone == null)) {
      state = false;
      wx.showToast({
        title: '法人手机号错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }
    var address = that.data.address
    if (address == "" || address == null) {
      state = false;
      wx.showToast({
        title: '地址错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }
    var registerPhone = that.data.registerPhone
    if (registerPhone == "" || registerPhone == null) {
      state = false;
      wx.showToast({
        title: '手机号错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }
    var password = that.data.password
    if (password == "" || password == null) {
      state = false;
      wx.showToast({
        title: '密码错误',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }
    var identification = that.data.identification
    if (userType == '0' && (identification == "" || identification == null)) {
      state = false;
      wx.showToast({
        title: '身份证号不能为空',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      return false;
    }else if (userType == '1' && (identification == "" || identification == null)) {
      wx.showToast({
        title: '企业代码不能为空',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      that.setData({
        accept: false
      })
      state = false;
      return false;
    }
    var wxUser = wx.getStorageSync('openid');
    //if (state) {
      wx.request({
        url: 'https://www.gcjxglzx.com/userInfo/save',
        method: 'POST',
        data: {
          registerName: registerName,
          userType: userType,
          identification: identification,
          spouseName: spouseName,
          spouseIdentification: spouseIdentification,
          spousePhone: spousePhone,
          address: address,
          registerPhone: registerPhone,
          password: password,
          wxUser: wxUser
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.status == '0') {
            wx.setStorageSync('identification', identification);
            wx.setStorageSync('password', password)
            wx.setStorageSync('registerName', '')
            wx.setStorageSync('registerPhone', '')
            wx.setStorageSync('address', '')
            wx.setStorageSync('spouseName', '')
            wx.setStorageSync('spouseIdentification', '')
            wx.setStorageSync('spousePhone', '')
            wx.redirectTo({
              url: '/page-personalInformationImg/home'
            })
          }
        }
      })
    //} 
    // else {
    //   wx.showModal({
    //     title: '注册失败!',
    //     content: '请填完整注册信息',
    //     showCancel: false,
    //     success: function (res) {
    //       if (res.confirm) {
    //         that.setData({
    //           accept: false,
    //         });
    //       }
    //     }
    //   })
    // }
  },

  reset: function (e) {
    this.setData({
      userType: '',
      registerName: '',
      identification: '',
      registerPhone: '',
      address: '',
      spouseName: '',
      spouseIdentification: '',
      spousePhone: '',
    })
    wx.setStorageSync('userType', '')
    wx.setStorageSync('registerName', '')
    wx.setStorageSync('identification', '')
    wx.setStorageSync('registerPhone', '')
    wx.setStorageSync('address', '')
    wx.setStorageSync('spouseName', '')
    wx.setStorageSync('spouseIdentification', '')
    wx.setStorageSync('spousePhone', '')
  },

  //页面加载初始化
  onLoad: function (options) {
    wx.setStorageSync('password', "")
    var code;
    wx.login({
      success: function (res1) {
        code = res1.code
        //1499893922商户号
        console.log("获取到的code：" + code)
        wx.request({
          //直接使用登录的code查看微信接口并返回openid和session_key
          // url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx363536b420e01cd1&secret=8bc23bed8f51078504ffc9656606e8c8&js_code=' + code + '&grant_type=authorization_code',


          //请求后台服务器查看微信接口并返回openid和session_key
          url: 'https://www.gcjxglzx.com/wxUserLog/login',
          data: {
            appid: 'wx363536b420e01cd1',
            secret: '8bc23bed8f51078504ffc9656606e8c8',
            js_code: code,
            grant_type: 'authorization_code'
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res2) {
            if (res2.data.status == '0') {
              wx.setStorageSync('openid', res2.data.data);
            }
          }
        })
      }
    })
    if (wx.getStorageSync('identification') != '' && wx.getStorageSync('identification') != null && wx.getStorageSync('password') != '' && wx.getStorageSync('password') != null){
      wx.redirectTo({
        url: '/page-personalInformationImg/home'
      })
    }else{
      this.setData({
        qylxr: true,
        infoData: this.data.spouse,
        userType: wx.getStorageSync('userType')
      })
      if (this.data.userType == '') {
        wx.setStorageSync('userType','0')
        this.setData({
          userType: wx.getStorageSync('userType')
        })
        if (this.data.userType == '0' && wx.getStorageSync('marryStatus') == '') {
          wx.setStorageSync('marryStatus', '0')
          this.setData({
            marryStatus: wx.getStorageSync('marryStatus'),
            marriage: true,
            infoData: this.data.spouse
          })
        }
      } else if (this.data.userType == '1') {
        wx.setStorageSync('userType', '1')
        this.setData({
          userType: wx.getStorageSync('userType'),
          qylxr: false,
          marriage: true,
          infoData: this.data.enterprise
        })
      }
      if (this.data.marryStatus != '' && this.data.marryStatus != '0') {
        this.setData({
          marriage: false
        })
      }
    }
  },

  onReady: function () {

  },

  onShow: function () {

  },


  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {

  },


  onReachBottom: function () {

  },

  onShareAppMessage: function () {

  },

})